//
//  AllAuto_View.m
//  HomePage
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "AllAuto_View.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"

@implementation AllAuto_View
@synthesize FlowView,autoarrays;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {

        ResourceLoader * loader = [ResourceLoader getInstance];
        [loader RefreshAutoDics];
        NSArray * autoArray = [loader getAutoArray];
        if ([autoArray count]==0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有资源，请下载!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            return self;
        }
        carmaxindex = [autoArray count]-1;
        
        self.autoarrays = [loader getHomePageAutoData];

        int height = (ImageViewSpaceTop+ImageViewHeight);
        
        FlowView = [[OverflowView alloc] initWithFrame:CGRectMake(0, (frame.size.height-height)/2, frame.size.width, height)];
        FlowView.delegate = self;
        FlowView.dataSource = self;
        FlowView.miniScale = 0.9;
        
        [self addSubview:FlowView];
        
        coverV = [[CoverView alloc] initWithFrame:self.bounds];
        [self addSubview:coverV];
        
        coverV.delegate =self;
        coverV.nFingerDistance = 90;
    }
    return self;
}


-(void)click{

    CGPoint clickPoint = [coverV convertPoint:coverV->orTouchBegin toView:FlowView];
    
    int i = [FlowView GetPageDistance:clickPoint];
    if (i>0) {
//        while (i>0) {
//            [FlowView toNextPage];
//            i--;
//        }
        [FlowView toNextPage:i];
    }
    else if(i<0)
    {
//        while (i<0) {
//            [FlowView toPreviousPage];
//            i++;
//        }
        [FlowView toPreviousPage:ABS(i)];
        
    }else{
        //@hanhaidi@0905@点击中间图片，进入详情页面
        [self EnterDetail];
    }
    
    if (i>carmaxindex) {
        return;
    }
}

-(void)EnterDetail
{
//    NSNumber * index = [NSNumber numberWithInt:FlowView->centerIndex];
    NSDictionary * dic = [autoarrays objectAtIndex:FlowView->centerIndex];
    [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_SecondPage object:dic userInfo:nil];
}

-(void)toNextPage{
    [FlowView toNextPage];
    
}
-(void)toPreviousPage{
    [FlowView toPreviousPage];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark OverflowView Delegate
-(void)CurrentIndexHasChange:(NSNumber *)num
{
    NSDictionary * dic = [autoarrays objectAtIndex:[num integerValue]];
    NSString * imagepath = [dic objectForKey:@"LogoPath"];
    [coverV SetLogoImage:imagepath];
}

////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark OverflowView Datasource

- (CGSize)sizeForPageInFlowView:(OverflowView *)flowView;{
    return CGSizeMake(ImageViewWidth, ImageViewHeight);
}

//返回显示View的个数
- (NSInteger)numberOfPagesInFlowView:(OverflowView *)flowView{
    return [autoarrays count];
}

//返回给某列使用的View
- (UIView *)flowView:(OverflowView *)flowView cellForPageAtIndex:(NSInteger)index{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.layer.cornerRadius = 6;
    imageView.layer.masksToBounds = YES;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    NSDictionary * dic = [autoarrays objectAtIndex:index];
    NSString * imagepath = [dic objectForKey:@"homeimage"];
    UIImage * image = [[UIImage alloc] initWithContentsOfFile:imagepath];
    if (image) {
        imageView.image = image;
        [image release];
    }
    
    return [imageView autorelease];
}


-(void)dealloc
{
    self.autoarrays = nil;
    [super dealloc];
}
@end
